﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class LarImage
    {
        private byte[] textureData;
        private int width;
        private int height;

        public byte[] TextureData
        {
            get 
            {
                textureData = new byte[4 * width * height];
                texture.GetData<byte>(textureData);
                return textureData;
            }
        }

        public int Width
        {
            get { return width; }
            set
            {
                texture = null;
                width = value;
            }
        }
        public int Height
        {
            get { return height; }
            set
            {
                texture = null;
                height = value;
            }
        }

        private Texture2D texture;
        public Texture2D Texture2D
        {
            get
            {
                if (texture == null)
                    GenerateTexture();
                return texture;
            }
        }

        public LarImage()
        {
        }
        
        public void SetTexture(Texture2D Tex)
        {
            texture = Tex;
            width = Tex.Width;
            height = Tex.Height;
        }
        public void GenerateTexture()
        {
            texture = new Texture2D(Engine.GraphicsDevice, width, height);
            texture.SetData<byte>(textureData);
        }

        #region Serializing

        /// <summary>
        /// Saves the Image
        /// </summary>
        /// <param name="Image">Image to be saved</param>
        /// <param name="FileName">Place to save it</param>
        public static void Save(LarImage Image, string FileName)
        {
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }

            FileStream Stream = new FileStream(FileName, FileMode.Create);
            
            BinaryWriter Writer = new BinaryWriter(Stream);
            Writer.Write(Image.Width);
            Writer.Write(Image.Height);
            Writer.Write(Image.TextureData, 0, Image.TextureData.Length);

            Stream.Flush();
            Stream.Close();

            Writer.Dispose();           
        }
        public static LarImage Load(string FileName, LarImageLoadOptions Options)
        {
            LarImage Image = null;
#if !WINDOWS_PHONE
            using (FileStream Stream = new FileStream(FileName, FileMode.Open, FileAccess.Read))
            {
#else
            using (Stream Stream = TitleContainer.OpenStream(FileName))
            {
#endif
                BinaryReader Reader = new BinaryReader(Stream);
                int width = Reader.ReadInt32();
                int height = Reader.ReadInt32();
                byte[] info = Reader.ReadBytes(4 * width * height);

                if (Options != null && Options.ColorKeyEnabled)
                {
                    Microsoft.Xna.Framework.Color col = Options.ColorKeyColor;
                    for (int i = 0; i < info.Length; i += 4)
                    {
                        if (info[i] == col.R &&
                            info[i + 1] == col.G &&
                            info[i + 2] == col.B &&
                            info[i + 3] == col.A)
                        {
                            info[i] = 0;
                            info[i + 1] = 0;
                            info[i + 2] = 0;
                            info[i + 3] = 0;
                        }
                    }
                }

                Texture2D texture = new Texture2D(Engine.GraphicsDevice, width, height);
                texture.SetData<byte>(info);
                Image = new LarImage();
                Image.SetTexture(texture);

#if !WINDOWS_PHONE
                Stream.Flush();
#endif
                Stream.Close();

                Reader.Dispose();
            }

            return Image;
        }

        #endregion
    }
}
